<?php
class Model_Cfg_SearchGroupItemShip extends Jwawa_BaseModel
{
    private static $validators = array(
        'search_group_id' => array(
            'rules' => array('Int' => null),
            'require' => true
        ),
        'search_item_id' => array(
            'rules' => array('Int' => null),
            'require' => true
        ),
        'seq' => array(
            'rules' => array('Int' => null),
            'require' => true
        ),
        'default_value' => array(
            'rules' => array(
                'StringLength' => array('max' => 50)
            )
        )
    );

    public static function findSearchGrpAndItemShipByPageId($pageId){
        $sql = "SELECT csgis.search_group_id,
                       csgis.search_item_id,
                       csgis.default_value
                FROM cfg_search_grp_itm_ships csgis
                JOIN cfg_search_groups csg on csg.id = csgis.search_group_id
                WHERE csg.page_id = :page_id
                ORDER BY csgis.seq ASC";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('page_id', $pageId);
        $stmt->execute();
        $rst = $stmt->fetchAllObj();

        return $rst;
    }

    public static function save($searchGroupItemShip){
        Jwawa_Validate_Factory::validate(self::$validators, $searchGroupItemShip);

        $sql = "INSERT INTO cfg_search_grp_itm_ships(search_group_id, search_item_id, seq, default_value)
                VALUES (:search_group_id, :search_item_id, :seq, :default_value)";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->setProperties($searchGroupItemShip);
//        $stmt->bindValue('search_group_id', $searchGroupItemShip->search_group_id);
//        $stmt->bindValue('search_item_id', $searchGroupItemShip->search_item_id);
//        $stmt->bindValue('seq', $searchGroupItemShip->seq);
//        $stmt->bindValue('default_value', $searchGroupItemShip->default_value);

        $rst = $stmt->execute();

        return $rst;
    }

    public static function deletBySearchGroupId($searchGroupIdList){
        if(empty($searchGroupIdList)){
            throw new Jwawa_BusinessException(500, 'searchGroupIds cannot be null');
        }

        for($i = 0; $i < count($searchGroupIdList); $i++){
            $validator = new Jwawa_Validate_Int();
            if(!$validator->isValid($searchGroupIdList[$i])){
                $message = implode(',\n', $validator->getMessages());
                throw new Jwawa_BusinessException('VALIDATE_ERROR', null,$message);
            }
        }

        $searchGroupIds = implode(',', $searchGroupIdList);
        $sql = "DELETE From cfg_search_grp_itm_ships
                WHERE search_group_id in ($searchGroupIds)";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->execute();
    }

    public static function deletBySearchItemId($searchItemId){
        if(empty($searchItemId)){
            throw new Jwawa_BusinessException(500, 'searchGroupIds cannot be null');
        }

        $sql = "DELETE From cfg_search_grp_itm_ships
                WHERE search_item_id = :search_item_id";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('search_item_id', $searchItemId);
        $stmt->execute();
    }
}
